/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:CommentServiceImpl.java
 * Version:1.0
 * Date:2011-12-10
 * Description: 
 * author:zhijian.zhang
 */
package com.tietiao.service.comment;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tietiao.common.util.PageModel;
import com.tietiao.common.util.StringUtil;
import com.tietiao.dao.UserCommentMapper;
import com.tietiao.dao.model.SysUser;
import com.tietiao.dao.model.UserComment;
import com.tietiao.dao.model.UserCommentCriteria;

@Service
public class CommentServiceImpl implements CommentService {

	@Resource
	private UserCommentMapper userCommentMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.comment.CommentService#addUserComment(java.lang.String
	 * , int, int, java.lang.Integer)
	 */
	@Override
	public int addUserComment(String comment, SysUser loginUser, int commodityId,
			Integer replyId) {
		UserComment userComment = new UserComment();
		userComment.setCommentContent(comment);
		userComment.setUserId(loginUser.getUserId());
		userComment.setPetName(loginUser.getPetName());
		userComment.setCommodityId(commodityId);
		userComment.setParentCommentId(replyId);
		userComment.setCommentTime(new Date());
		userCommentMapper.insertSelective(userComment);
		return userComment.getCommentId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.comment.CommentService#deleteUserCommentByKey(int)
	 */
	@Override
	public int deleteUserCommentByKey(int userCommentId) {
		return userCommentMapper.deleteByPrimaryKey(userCommentId);
	}

	@Override
	public PageModel<UserComment> getUserCommentPages(int commodityId,
			int pageNo, int pageSize) {
		UserComment userComment = new UserComment();
		userComment.setCommodityId(commodityId);

		UserCommentCriteria criteria = new UserCommentCriteria();
		criteria.createCriteria().andCommodityIdEqualTo(commodityId);
		return null;
	}

	@Override
	public PageModel<UserComment> getUserComments(UserComment userComment,
			int start, int limit, String sort, String dir) {
		List<UserComment> userComments = userCommentMapper
				.selectBgUserComments(userComment, start, limit,
						StringUtil.wrapSort(sort, dir));
		int count = userCommentMapper.selectBgUserCommentCounts(userComment);
		PageModel<UserComment> result = new PageModel<UserComment>(
				userComments, count);
		return result;
	}

	@Override
	public int delUserComments(Integer[] commentIds) {
		UserCommentCriteria criteria = new UserCommentCriteria();
		UserCommentCriteria.Criteria criterias = criteria.createCriteria();

		if (null == commentIds || commentIds.length == 0) {
			return 0;
		}
		criterias.andUserIdIn(Arrays.asList(commentIds));
		return userCommentMapper.deleteByExample(criteria);
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.comment.CommentService#getCommentInfoByCommodityId(int)
	 */
	@Override
	public List<UserComment> getCommentInfoByCommodityId(int commodityId, String orderByClause,int parentCommentId) {
		return userCommentMapper.getCommentInfoByCommodityId(commodityId, orderByClause,parentCommentId);
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.comment.CommentService#addContentInfo(com.tietiao.dao.model.SysUser, int, java.lang.String, java.lang.String)
	 */
	@Override
	public int addContentInfo(SysUser loginUser, int commodityId,
			String commentId, String contentInfo) {
		UserComment userComment = new UserComment();
		userComment.setCommentContent(contentInfo);
		userComment.setUserId(loginUser.getUserId());
		userComment.setPetName(loginUser.getPetName());
		userComment.setCommodityId(commodityId);
		userComment.setParentCommentId(Integer.parseInt(commentId));
		userComment.setCommentTime(new Date());
		userCommentMapper.insertSelective(userComment);
		return userComment.getCommentId();
	}

}
